package cn.owng.server.platform.service.impl;

import cn.owng.server.common.generated.entity.Game;
import cn.owng.server.common.generated.service.IGameService;
import cn.owng.server.platform.model.dto.GameDTO;
import cn.owng.server.platform.model.http.request.GameGameSaveOrUpdateReq;
import cn.owng.server.platform.model.http.request.PaginateReq;
import cn.owng.server.platform.service.EntityToDtoMapper;
import cn.owng.server.platform.service.IGameGameService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@Service
public class GameGameServiceImpl implements IGameGameService {

    /**
     * Mybatis plus 自动生成的service
     */
    private final IGameService gameService;

    public GameGameServiceImpl(IGameService gameService) {
        this.gameService = gameService;
    }

    @Override
    public IPage<GameDTO> paginate(PaginateReq paginateReq) {
        LambdaQueryWrapper<Game> queryWrapper = Wrappers.lambdaQuery(Game.class);
        Map<String, Object> search = paginateReq.getSearch();
        String name = (String) search.get("name");
        queryWrapper.like(StringUtils.isNotBlank(name), Game::getName, name);
        queryWrapper.orderByDesc(Game::getDateModified);
        Page<Game> page = this.gameService.page(new Page<>(paginateReq.getCurrentPage(), paginateReq.getPageSize()), queryWrapper);
        return page.convert(EntityToDtoMapper.INSTANCE::gameToDTO);
    }

    @Override
    public boolean delete(Long gameId) {
        return this.gameService.removeById(gameId);
    }

    @Override
    public GameDTO saveOrUpdate(GameGameSaveOrUpdateReq gameGameSaveOrUpdateReq) {
        Long gameId = gameGameSaveOrUpdateReq.getGameId();
        Game game = gameId == null ? null : this.gameService.getById(gameId);
        if (game == null) game = new Game().setDateAdded(new Date());
        this.gameService.saveOrUpdate(
                game.setCode(gameGameSaveOrUpdateReq.getCode())
                        .setName(gameGameSaveOrUpdateReq.getName())
                        .setDescription(gameGameSaveOrUpdateReq.getDescription())
                        .setBanner(gameGameSaveOrUpdateReq.getBanner())
                        .setIcon(gameGameSaveOrUpdateReq.getIcon())
                        .setDeployment(gameGameSaveOrUpdateReq.getDeployment())
                        .setSortOrder(Integer.valueOf(gameGameSaveOrUpdateReq.getSortOrder()))
                        .setStatus(Integer.valueOf(gameGameSaveOrUpdateReq.getStatus()))
                        .setDateModified(new Date()));
        return EntityToDtoMapper.INSTANCE.gameToDTO(game);
    }
}
